package com.atguigu.system.service.impl;

import com.atguigu.common.helper.MenuHelper;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysRoleMenu;
import com.atguigu.model.vo.AssginMenuVo;
import com.atguigu.system.exception.GuiguException;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.mapper.SysRoleMapper;
import com.atguigu.system.mapper.SysRoleMenuMapper;
import com.atguigu.system.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findMenuNodes() {
        //调用SysMenuMapper中查询所有的方法
        List<SysMenu> sysMenus = sysMenuMapper.selectList(null);
        //通过MenuHelper工具类将所有的菜单转换为菜单树
        List<SysMenu> menuTree = MenuHelper.buildTree(sysMenus);
        return menuTree;
    }

    @Override
    public void deleteEmptyNode(Long id) {
        //根据id判断当前节点是否有子节点
        Integer count = sysMenuMapper.selectCount(new QueryWrapper<SysMenu>().eq("parent_id", id));
        //判断
        if(count > 0){
            //该节点有子节点，不能删除，抛出自定义异常
            throw new GuiguException(ResultCodeEnum.NODE_ERROR);
        }
        //删除该节点
        sysMenuMapper.deleteById(id);
    }

    @Override
    public List<SysMenu> getRoleMenuList(Long roleId) {
        //获取所有权限菜单
        List<SysMenu> menuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().eq("status", 1));
        //根据角色id查询中间表获取该角色已分配的权限菜单
        List<SysRoleMenu> roleMenuList = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));
        //创建一个List保存角色已分配的权限菜单的id
        List<Long> roleMenuIds = new ArrayList<>();
        //遍历该角色已分配的权限
        for (SysRoleMenu sysRoleMenu : roleMenuList) {
            roleMenuIds.add(sysRoleMenu.getMenuId());
        }
        //遍历所有的权限菜单
        for (SysMenu sysMenu : menuList) {
            //获取当前权限菜单的id
            Long menuId = sysMenu.getId();
            //判断roleMenuIds中是否包含当前权限菜单的id
            if(roleMenuIds.contains(menuId)){
                //证明该权限菜单已被该角色分配，设置被选中
                sysMenu.setSelect(true);
            }
        }
        //将权限菜单转换为菜单树
        List<SysMenu> menuTree = MenuHelper.buildTree(menuList);
        return menuTree;
    }

    @Override
    public void assignMenu(AssginMenuVo assginMenuVo) {
        //获取角色id
        Long roleId = assginMenuVo.getRoleId();
        //根据角色操作中间表删除之前分配的权限菜单
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().eq("role_id",roleId));
        //获取现在分配的所有的权限菜单的id值
        List<Long> menuIdList = assginMenuVo.getMenuIdList();
        //重新分配权限
        for (Long menuId : menuIdList) {
            if(menuId != null){
                //创建SysRoleMenu对象
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                //设置角色id
                sysRoleMenu.setRoleId(roleId);
                //设置权限菜单id
                sysRoleMenu.setMenuId(menuId);
                //保存
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        }
    }
}
